//
//  NewfeatureViewController.m
//  LoveFreshBeen
//
//  Created by 辛路琦 on 16/6/11.
//  Copyright © 2016年 xinluqi. All rights reserved.
//

#import "NewfeatureViewController.h"
#import "NewfeatureViewCell.h"

#define NewfeatureImageViewCount 4
@interface NewfeatureViewController ()

@end

@implementation NewfeatureViewController


- (instancetype)init {
    
    // 1.创建layout对象
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    // 2.设置layout对象
    // 让cell和屏幕一样宽一样高
    layout.itemSize = [UIScreen mainScreen].bounds.size;
    
    // 设置水平滚动
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    // 设置间隙
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    
    if (self = [super initWithCollectionViewLayout:layout]) {
        
    }
    
    return self;
}


static NSString * const reuseIdentifier = @"NewfeatureViewCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 1.注册cell
    [self.collectionView registerClass:[NewfeatureViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    // 2.设置collectionView的相关属性
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.bounces = NO;
    self.collectionView.pagingEnabled = YES;
    
}


#pragma mark <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return NewfeatureImageViewCount;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NewfeatureViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.index = (int)indexPath.item + 1;
    [cell hiddenStartButton:YES];
    // 控制开始按钮的显示和隐藏
    if (indexPath.item == (NewfeatureImageViewCount - 1)) {
        [cell hiddenStartButton:NO];
    }
    
    return cell;
}

#pragma mark <UICollectionViewDelegate>

// cell完全显示就会调用
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    
    // 获取当前显示的cell对应的索引
    NSIndexPath *path = collectionView.indexPathsForVisibleItems.lastObject;
    
    // 根据索引获取对应页的cell, 然后调用cell的方法执行动画
    NewfeatureViewCell *myCell = [collectionView cellForItemAtIndexPath:path];
    
    // 判断是否是最后一页
    if (path.item == (NewfeatureImageViewCount - 1)) {
        [myCell startButtonAnim];
    }
    
}

@end
